package com.stone.business.gzrwd.action;

/**
 * <pre>
 * 变电运行工区工作任务单 
 * @author
 * </pre>
 */

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpSession;

import net.sf.json.JSON;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;

import com.stone.business.gzrwd.pojo.TGzrwd;
import com.stone.business.gzrwd.service.ZbjlCommonService;
import com.stone.para.IConstant;
import com.stone.sys.pojo.TOrgan;
import com.stone.sys.pojo.TUser;
import com.stone.utils.JsonUtils;
import com.stone.utils.StringUtils;

import frm.ssh.action.BaseListAction;
import frm.ssh.util.page.Page;

public class GzrwdListAction extends BaseListAction {

	private static final long serialVersionUID = 3979577965804736350L;

	private ZbjlCommonService myService;
	private Page pageInfo1;
	String month;
	String currentmonth;
    String ym;
    String monthid;
  //班组
	private List<TOrgan> bzList;
		public List<TOrgan> getBzList() {
		return bzList;
	}

	public void setBzList(List<TOrgan> bzList) {
		this.bzList = bzList;
	}

		public String getMonth() {
			return month;
		}

		public void setMonth(String month) {
			this.month = month;
		}

		public String getCurrentmonth() {
			return currentmonth;
		}

		public void setCurrentmonth(String currentmonth) {
			this.currentmonth = currentmonth;
		}

		public String getMonthid() {
			return monthid;
		}

		public void setMonthid(String monthid) {
			this.monthid = monthid;
		}
	// 无票列表
	private List<TGzrwd> listWp = null;
	public ZbjlCommonService getMyService() {
		return myService;
	}

	public void setMyService(ZbjlCommonService myService) {
		this.service = myService;
		this.myService = myService;
	}
	
    /** * 构造方法. */
    public GzrwdListAction() {
        this.entityClass = TGzrwd.class;
    }
private Long planid;
	public Long getPlanid() {
	return planid;
}

public void setPlanid(Long planid) {
	this.planid = planid;
}

	/**
	 * 设置查询条件
	 *//*
	public HashMap<String, Object> setSearchCondition() {
		
		HashMap<String, Object> m = new HashMap<String, Object>();
		
		List<Object> valsList = new ArrayList<Object>();
		
//		String organCode = String.valueOf(super.getSession().getAttribute(IConstant.SESSION_TORGAN_CODE));

		String organCode = String.valueOf(super.getSession().getAttribute(IConstant.SESSION_TORGAN_CODE));
	//	String isLike = String.valueOf(super.getSession().getAttribute(IConstant.SESSION_ISLIKE));
		
		String isLike ="like";
		m.put("cols", "new map(s.id as id, s.rq as rq,s.pcsj as pcsj, s.bh as bh, zbzname as zbzname, zbyname as zbyname, s.ycclqk as gzfw, case when bgzr='21' then '保护班' when bgzr='22' then '检修班' when bgzr='23' then '变压器班' when bgzr='24' then '试验班' else '' end as bzname,s.czkssj as czkssj,s.czjssj as czjssj,s.bdzname as bdzname,s.sfwc as sfwc)");
		
//		// 查询条件OUID很重要
//		String conditions = " s.ouid like '" + organCode + "%'";
	String conditions = " state = '" + super.getSearchBean().getCon1() + "'"; 
//		 查询条件OUID很重要
		//String conditions = " s.ouid "+isLike+" '" + organCode + "%'";
		//String conditions += " and state = '" + super.getSearchBean().getCon1() + "'";
		
		// 巡视时间
		if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon2())) {
			conditions += " AND TO_CHAR(s.rq,'yyyy-MM-dd') >= ?";
			valsList.add(super.getSearchBean().getCon2());
		}
		
		if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon3())) {
			conditions += " AND TO_CHAR(s.rq,'yyyy-MM-dd') <= ?";
			valsList.add(super.getSearchBean().getCon3());
		}
		
		// 变电站
		if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon4())) {
			conditions += " AND s.sfwc = ?";
			valsList.add(super.getSearchBean().getCon4());
		}
		// 工作计划下的任务单
		if (planid!=null) {
			conditions += " AND s.planid = ?";
			valsList.add(planid);
		}
		// 查询条件
		m.put("conditions",conditions);
		m.put("clause", " order by s.czkssj Desc");
		m.put("alias", "s");//, TOrgan o
		m.put("vals", valsList.toArray());
		
	//	DictionaryService dicService = (DictionaryService) SpringUtil.getInstance().getBean("dicService");
	//	xslxList = dicService.findList("XSLX");
		HttpSession session= super.getSession();
		return m;
		
	}*/
/**
 * 设置查询条件
 */
public HashMap<String, Object> setSearchCondition() {
	
	HashMap<String, Object> m = new HashMap<String, Object>();
	
	List<Object> valsList = new ArrayList<Object>();
	StringBuffer sql = new StringBuffer();
	sql.append(" select s.id as id,");
	sql.append("	 s.rq as rq,s.pcsj as pcsj, s.bh as bh, dwryname as dwryname,zbzname as zbzname, zbyname as zbyname, s.cxch as cxch,s.gs as gs,s.wygs as wygs,nvl(s.remark,' ') as remark,nvl(s.ycclqk,' ') as gzfw, (select t.organ_name from sys_organ t where t.id=s.bgzr) as bzname,s.czkssj as czkssj,s.czjssj as czjssj,s.bdzname as bdzname,s.sfwc as sfwc,");
	sql.append("	(select REPLACE(ltrim(sys_connect_by_path(REPLACE(gznr,id, ''), '|'), '|'),'|','★') as gznr");
	sql.append("		from (select GZRWDID, id, (id ||sbname|| gznr) as gznr , count(*) over(partition by GZRWDID) cnt from TAB_GZRWD_RW d order by id) ss");
	sql.append("		where level = cnt");
	sql.append("		and ss.GZRWDID = s.id");
	sql.append("		connect by prior GZRWDID = GZRWDID");
	sql.append("		and prior (id || gznr) < (id || gznr)) as rwnr,");
	//start 工作任务增加控制卡状态栏、工序卡状态栏  added by cheng 2012-5-14
	sql.append(" s.gzpno AS gzpno, s.planid AS planid, ");
	sql.append("nvl((select tk.sh_status from tab_kzk tk where s.planid = tk.planid), '0') as kzkzt,");
	sql.append("nvl((select tk.oc_type from tab_kzk tk where s.planid = tk.planid), '0') as kzklx,");//已存在控制卡类型防止打开混乱一个计划下只能有一个控制卡 by add wangtao20120531
		//工序卡状态
	sql.append(" nvl((select case tg.oc_status when '1' then ");
	sql.append(" (select case concat(max(oc_gzzt), min(oc_gzzt)) ");
	sql.append("   when '0303' then '5'  when '0101' then '6' else '7' end ");
	sql.append("  from tab_gxzlkzk_bz tgbz where tgbz.p_idsno = tg.id_sno) ");
	sql.append(" else tg.oc_status end ");
	sql.append(" from tab_gxzlkzk tg where s.planid = tg.planid), ");
	sql.append(" '0') as gxkzt, ");
	//end
	//领料单状态 
	sql.append(" nvl((select t.status from tab_cllyjl t where s.id = t.gzrwdid), '0') as ckdzt");
	//end
	sql.append("	from TAB_GZRWD s ");
	String conditions = " where s.state = '" + super.getSearchBean().getCon1() + "'"; 
	 //是否从个人待办事项进入任务单列表 added by wangtao 2012-5-25
	TUser loginUser  = (TUser)super.getSession().getAttribute(IConstant.SESSION_TUSER);//系统当前登录用户
	 if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon19()) && "grdbsx".equals(super.getSearchBean().getCon19())){
		 conditions += " AND (s.zbzname = '"+loginUser.getName()+ "'  or  s.zbyname like '%"+loginUser.getName()+ "%')" ;
		 }
	// 工作日期
	if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon2())) {
		conditions += " AND TO_CHAR(s.rq,'yyyy-MM-dd') >= ?";
		valsList.add(super.getSearchBean().getCon2());
	}
	
	if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon3())) {
		conditions += " AND TO_CHAR(s.rq,'yyyy-MM-dd') <= ?";
		valsList.add(super.getSearchBean().getCon3());
	}
	
	// 是否完成
	if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon4())) {
		conditions += " AND s.sfwc = ?";
		valsList.add(super.getSearchBean().getCon4());
	}
	if(loginUser==null){//检修网浏览
		HttpSession s = super.getRequest().getSession(false);
		Enumeration enm = super.getRequest().getSession(false).getAttributeNames();
		Object attribute = null;
		
		/*// 清除SESSION
		while (enm.hasMoreElements()) {
			attribute = enm.nextElement();

			if (!attribute.equals(IConstant.SESSION_TORGAN)
				&& !attribute.equals(IConstant.SESSION_TORGAN_CODE)
				&& !attribute.equals(IConstant.SESSION_TUSER)
				&& !attribute.equals(IConstant.SESSION_ISADMIN)){

				try {
					s.removeAttribute((String)attribute);
				} catch (Exception e) {
					
				}
			}
		}*/
		
		List<TUser> list = myService.findListByHql("from TUser where loginId = 'guest'") ;
		
		TUser user = list.get(0);
				
		// 是否管理员（1是,0不是）
		this.setSessionAttribute(IConstant.SESSION_ISADMIN, user.getIsadmin());
		// 用户信息放入SESSION
		this.setSessionAttribute(IConstant.SESSION_TUSER, user);
		//工作任务单初始当日工单。
		super.getSearchBean().setCon5("01");
		
	}
	//System.out.println("super.getSearchBean().getCon5()=="+super.getSearchBean().getCon5());
	// 当日明日
	if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon5())) {
		
		if(super.getSearchBean().getCon5().equals("01")){
			conditions += " AND TO_CHAR(s.rq,'yyyy-MM-dd') = TO_CHAR(sysdate,'yyyy-MM-dd')";
		}else{
			conditions += " AND TO_CHAR(s.rq,'yyyy-MM-dd') = TO_CHAR(sysdate+1,'yyyy-MM-dd')";
		}
		
	}
	// 班组
	if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon18())) {
		conditions += " AND s.bgzr = '"+super.getSearchBean().getCon18()+"'";
		
	}
	
	if (!StringUtils.isNullOrBlank(super.getSearchBean().getCon11())) {
		conditions += " and to_char(s.rq,'YYYY-MM') = '" + super.getSearchBean().getCon11() + "'";
	}
	// 工作计划下的任务单
	if (planid!=null) {
		conditions += " AND s.planid = ?";
		valsList.add(planid);
	}
	m.put("vals", valsList.toArray());
	m.put("pageflag", "sql");
	m.put("sql", sql.toString() + conditions + "  order by s.rq Desc");
	//////////////by wangtao add20120516无票

	HashMap<String, Object> m1 = new HashMap<String, Object>();
	StringBuffer sql1 = new StringBuffer();
	sql1.append(" select s.id as id,");
	sql1.append("	 s.rq as rq,s.pcsj as pcsj, s.bh as bh, zbzname as zbzname, zbyname as zbyname, s.cxch as cxch,s.gs as gs,s.ycclqk as gzfw, (select t.organ_name from sys_organ t where t.id=s.bgzr) as bzname,TO_CHAR(s.czkssj,'YYYY-MM-DD HH24:MI') as czkssj,TO_CHAR(s.czjssj,'YYYY-MM-DD HH24:MI') as czjssj,s.bdzname as bdzname,s.sfwc as sfwc,s.wcqk as wcqk,s.ylwt as ylwt ");
	sql1.append("	from TAB_GZRWD s ");
	String conditions1 = "";
	if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon1())) {
		if ("0".equals(super.getSearchBean().getCon1())) {
      conditions1 = " where s.state = '5'"; //无票初始状态为5,历史状态为6
		}else if ("9".equals(super.getSearchBean().getCon1())){
			 conditions1 = " where s.state = '6'";
		}
	}
	
	 //是否从个人待办事项进入任务单列表 added by wangtao 2012-5-25
	 if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon19()) && "grdbsx".equals(super.getSearchBean().getCon19())){
		 conditions1 += " AND (s.zbzname = '"+loginUser.getName()+ "'  or  s.zbyname like '%"+loginUser.getName()+ "%')" ;
		 }
	// 开始时间
	if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon2())) {
		conditions1 += " AND TO_CHAR(s.czkssj,'yyyy-MM-dd') >= '"+super.getSearchBean().getCon2()+"'";
		
	}
	
	if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon3())) {
		conditions1 += " AND TO_CHAR(s.czkssj,'yyyy-MM-dd') <= '"+super.getSearchBean().getCon3()+"'";
		
	}
	if (!StringUtils.isNullOrBlank(super.getSearchBean().getCon11())) {//工作任务单历史树用
		conditions += " and to_char(s.czkssj,'YYYY-MM') = '" + super.getSearchBean().getCon11() + "'";
	}
	
	// 当日明日
	if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon5())) {
		
		if(super.getSearchBean().getCon5().equals("01")){
			conditions1 += " AND (TO_CHAR(s.czkssj,'yyyy-MM-dd') <= TO_CHAR(sysdate,'yyyy-MM-dd') and TO_CHAR(s.czjssj,'yyyy-MM-dd') >= TO_CHAR(sysdate,'yyyy-MM-dd'))";
		}else{
			conditions1 += " AND (TO_CHAR(s.czkssj,'yyyy-MM-dd') <= TO_CHAR(sysdate+1,'yyyy-MM-dd') and TO_CHAR(s.czjssj,'yyyy-MM-dd') >= TO_CHAR(sysdate+1,'yyyy-MM-dd'))";
		}
		
	}
	// 班组
	if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon18())) {
		conditions1 += " AND s.bgzr = '"+super.getSearchBean().getCon18()+"'";
		
	}
	if (!StringUtils.isNullOrBlank(super.getSearchBean().getCon11())) {
		conditions1 += " and to_char(s.rq,'YYYY-MM') = '" + super.getSearchBean().getCon11() + "'";
	}
	   /* Page page = new Page();
		page.setStartRs(Integer.parseInt("0"));
		page.setPerPage(Integer.parseInt("100"));
		page.setObject(new HashMap());
	   listWp = myService.getPageListBySQL(page, sql1.toString() + conditions1 + "  order by s.rq Desc");*/
	  listWp = myService.findListBySql(sql1.toString() + conditions1 + "  order by s.rq Desc");
      listWp = setResultList(listWp);
     setReqestAttribute("listWp", listWp);
	////////////////////////////////////////////////
   //班组
 	String bzsql = "from TOrgan where id <=24 and id >=21 order by id";
 	bzList = myService.findListByHql(bzsql);
	return m;
	
}
/**
 * 获得左侧树
 */
public String toMonthTree() {
	
	List<HashMap<String, Object>> l = myService.findListBySql("select id as monthid, to_char(czkssj, 'YYYY-MM') as currentmonth from tab_gzrwd where TO_CHAR(czkssj, 'yyyy-MM') = TO_CHAR(sysdate, 'yyyy-MM')  ");
	if(l.size()!=0){
	currentmonth = l.get(0).get("currentmonth").toString();
	monthid = l.get(0).get("monthid").toString();
	}
	return "TREE_SUCCESS";
}
public void getMonthDate() throws Exception {
	
	List<HashMap<String,Object>> listTree;
	
	// 左侧的树
	if ("-1".equals(month)) {
		
		listTree = myService.findListBySql("select to_char(czkssj,'YYYY') as text, to_char(czkssj,'YYYY') as id, 0 as leaf from tab_gzrwd group by to_char(czkssj,'YYYY') order by id");

	// 维护时根节点
	} else if (month.length() == 4) {
		listTree = myService.findListBySql("select to_char(czkssj,'MM') as text, '" + month + "' || '-' || to_char(czkssj,'MM') as id, 1 as leaf  from tab_gzrwd where to_char(czkssj,'YYYY') = '" + month + "' group by to_char(czkssj,'MM') order by id");
	} else {
		listTree = myService.findListBySql("select to_char(czkssj,'MM') as text, '" + month + "' || '-' || to_char(czkssj,'MM') as id, 1 as leaf  from tab_gzrwd where to_char(czkssj,'YYYY') = '" + month + "' group by to_char(czkssj,'MM') order by id");
	}
	
    JsonConfig jsonConfig = JsonUtils.configJson(new String[] {}, "yyyy-MM-dd");
	
    JSON json = JSONSerializer.toJSON(listTree, jsonConfig);
    
    super.getResponse().getWriter().print(json.toString());
}
public Page getPageInfo1() {
	return pageInfo1;
}


public List<TGzrwd> getListWp() {
	return listWp;
}

public void setListWp(List<TGzrwd> listWp) {
	this.listWp = listWp;
}

}
